var service = new EbeimService();
var service2 = new EbeimService2();
var $dsType = $("#h_dsType").val();
$(function(){
	setInterval("getUpDownloadFaileds()",2500);
	setInterval("countByType(0)",2500);
	setInterval("countByType(1)",2500);
	
	$(".j-page-first").click(function(){
		if($(this).hasClass("on")){
			page("first");
		}
	});
	$(".j-page-prev").click(function(){
		if($(this).hasClass("on")){
			page("prev");
		}
	});
	$(".j-page-next").click(function(){
		if($(this).hasClass("on")){
			page("next");
		}
	});
	$(".j-page-last").click(function(){
		if($(this).hasClass("on")){
			page("last");
		}
	});
});
var $uploadFailed = 0;
var $downloadFailed = 1;

var $currentpage = 1;
var $offset = 0;
var $totalpage = 1;
var $total = 0;
var $failed_pagesize = 10;
function showUpDownloadFaileds(flag){
	showDialog("upDownloadAgainWindow");
	if(flag == $uploadFailed){
		$("#upDownloadAgainWindow .dialogTipContentSpan").html("上传失败记录");
		$("#tryAgainBtn").html("重新上传")
		$("#tryAgainBtn").parent().attr("tryAgainType",$uploadFailed);
		service2.getUpDownloadFileFailedsByPage({
			type:$uploadFailed,
			dsType:$dsType,
			offset:$offset,
			pagesize:$failed_pagesize
		}, showUpDownloadFailedsCB);
	}else if(flag == $downloadFailed){
		$("#upDownloadAgainWindow .dialogTipContentSpan").html("下载失败记录");
		$("#tryAgainBtn").html("重新下载")
		$("#tryAgainBtn").parent().attr("tryAgainType",$downloadFailed);
		service2.getUpDownloadFileFailedsByPage({
			type:$downloadFailed,
			dsType:$dsType,
			offset:$offset,
			pagesize:$failed_pagesize
		}, showUpDownloadFailedsCB);
	}
}
function countByType(type){
	service.countByType({
		type:type,
		dsType:$dsType
	},function(data){
		if(data.type==$uploadFailed){
			if(data.count > 0){
				var jtag = $("#upload_failed_tag").siblings(".j-tag");
				$("#upload_failed_tag").css("display","block");
				$("#upload_failed_tag").html(data.count);
				jtag.css("display","block");
				jtag.html(data.count);
				jtag.stop();
				jtag.animate({padding:"8px",top:"-10px",right:"-5px",opacity:"0"},500,function(){
					$(this).css({padding:"3px",top:"-5px",right:"0",display:"none",opacity:"1"});
				})
			}else{
				$("#upload_failed_tag").css("display","none");
			}
		}else if(data.type==$downloadFailed){
			if(data.count > 0){
				var jtag = $("#download_failed_tag").siblings(".j-tag");
				$("#download_failed_tag").css("display","block");
				$("#download_failed_tag").html(data.count);
				jtag.css("display","block");
				jtag.html(data.count);
				jtag.stop();
				jtag.animate({padding:"8px",top:"-10px",right:"-5px",opacity:"0"},500,function(){
					$(this).css({padding:"3px",top:"-5px",right:"0",display:"none",opacity:"1"});
				})
			}else{
				$("#download_failed_tag").css("display","none");
			}
		}
	});
}

function getUpDownloadFaileds(){
	var tryType = $("#tryAgainBtn").parent().attr("tryAgainType");
	if(tryType==undefined||tryType==""){
		tryType = 0;
	}
	service2.getUpDownloadFileFailedsByPage({
		type:tryType,
		dsType:$dsType,
		offset:$offset,
		pagesize:$failed_pagesize
	}, showUpDownloadFailedsCB);
}
function page(flag) {

	if (flag == "first") {
		$currentpage = 1;
	} else if (flag == "last") {
		$currentpage = $totalpage;
	} else if (flag == "prev") {
		if ($currentpage - 1 <= 0) {
			return;
		}
		$currentpage--;
	} else if (flag == "next") {
		if ($currentpage + 1 > $totalpage) {
			return;
		}
		$currentpage++
	} else if (!isNaN(flag)) {
		flag = flag.substring(0,flag.indexOf("."));
		if (flag > $totalpage || flag < 1) {
			plugin.msgtip({type:"e",msg:"超出范围!"});
			return;
		}
		$currentpage = flag * 1;
	}
	$("#w-page").val($currentpage);
	$offset = ($currentpage - 1) * $failed_pagesize;
	var tryType = $("#tryAgainBtn").parent().attr("tryAgainType");
	showUpDownloadFaileds(tryType);
}
var showUpDownloadFailedsCB = function(data){
	var tryType = $("#tryAgainBtn").parent().attr("tryAgainType");
	var uploadFailedContent = "";
	$total = data.total ? data.total : 1;
	$totalpage = Math.ceil($total / $failed_pagesize);
	if($currentpage >= $totalpage){
		$(".j-page-next").removeClass("on");
		$(".j-page-last").removeClass("on");
	}else{
		$(".j-page-next").addClass("on");
		$(".j-page-last").addClass("on");
	}
	if($currentpage == 1){
		$(".j-page-prev").removeClass("on");
		$(".j-page-first").removeClass("on");
	}else{
		$(".j-page-prev").addClass("on");
		$(".j-page-first").addClass("on");
	}
	$("#c-page").html($currentpage);
	$("#t-page").html($totalpage == 0 ? 1 : $totalpage);
	for(var i=0;i<data.beans.length;i++){
		var bean = data.beans[i];
		var dpb = bean.downloadProcessBarBean;
		var status = bean.status;
		status = getStatusExp(tryType, status);
		var path=bean.root+bean.path+"\\"+bean.name;
		path=path.replace(/\\\//g,'');
		if (path.substr(0,1)=='\\') {
			path=path.substr(1);
		}
		if (path.substr(0,1)=='\\') {
			path=path.substr(1);
		}
			
		if(tryType==$uploadFailed){
			uploadFailedContent = uploadFailedContent
			+"<tr>"
			+"	<td title='"+path+"' style='border-bottom:solid 1px #ddd;padding:10px 0'>"
			+		"<input type='hidden' class='failedClass' value='"+bean.id+"' />"
			+"		<nobr style='max-width:400px;display:inline-block;overflow:hidden;text-overflow:ellipsis;'>"
			+path
			+"		</nobr>"
			+"	</td>"
			+"	<td>"
			+status
			+"	</td>"
			+"</tr>";
		}else if(tryType==$downloadFailed){
			var maxWidth = "400px";
			if(dpb!=undefined){
				maxWidth = "250px";
			}
			if(dpb!=undefined){
				var total = dpb.total;
				var count = dpb.count;
				var sum = dpb.fileSum;
				var ok = dpb.okSum;
				var error = dpb.errorSum;
				var p = (count*100/total).toFixed(2);
				var info = "";
				var barColor = "green";
				if(error==1){
					info = "<span style='color:red'>下载失败</span>"
					barColor = "#999";
					$(".j-clear").css("display","inline-block");
					$(".j-unclear").css("display","none");
				}else if(ok==1 || count>=total){
					info = "下载成功"
					$(".j-clear").css("display","inline-block");
					$(".j-unclear").css("display","none");
				}else{
					info = p+"%";
				}
//				if(bean.path == "\/"){
//					bean.path = "";
//				}
				uploadFailedContent = uploadFailedContent
				+"<tr>"
				+"	<td title='"+path+"' style='border-bottom:solid 1px #ddd;padding:10px 0'>"
				+		"<input type='hidden' class='failedClass' value='"+bean.id+"' />"
				+"		<nobr title='"+bean.name+"' style='width:"+maxWidth+";display:inline-block;overflow:hidden;text-overflow:ellipsis;'>"
				+path
				+"		</nobr>"
				+"	</td>"
				+"	<td width='150px' style='text-align:center;' class='processTD'>"
				+"<div class='processbar' style='background:#999;width:100px;height:20px;position:relative;margin:0 auto;'>"
				+ "<div class='bar' style='height:20px;background:"+barColor+";width:"+p+"%;'></div>"
				+ "<div class='info' style='font-size:12px;width:100px;height:20px;color:#fff;position:absolute;top:0;left:0'>"
				+	"已下载:"+info
				+" </div>"
				+"</div>"
				+"	</td>"
				+"	<td>"
//				+status
				+"	</td>"
				+"</tr>";
			}else{
				uploadFailedContent = uploadFailedContent
				+"<tr>"
				+"	<td title='"+path+"' style='border-bottom:solid 1px #ddd;padding:10px 0'>"
				+		"<input type='hidden' class='failedClass' value='"+bean.id+"' />"
				+"		<nobr style='width:"+maxWidth+";display:inline-block;overflow:hidden;text-overflow:ellipsis;'>"
				+path
				+"		</nobr>"
				+"	</td>"
				+"	<td style='text-align:center;'>"
				+status
				+"	</td>"
				+"</tr>";
			}
			
		}
	}
	$("#upDownloadAgainTB").html(uploadFailedContent);
}
function getStatusExp(tryType,status){
	if(tryType == $uploadFailed){
		if(status==0){
			status = "<font color=green>可重新上传</font>"
			$(".j-clear").css("display","inline-block");
			$(".j-unclear").css("display","none");
		}else if(status==1){
			status = "<font color=gray>队列中...</font>"
		}else if(status==-1){
			status = "<font color='#65A324'>上传中...</font>"
		}else if(status==2){
			status = "<font color=green>上传成功!</font>"
			$(".j-clear").css("display","inline-block");
			$(".j-unclear").css("display","none");
		}else if(status==3){
			status = "<font color=red>重新上传失败!</font>"
			$(".j-clear").css("display","inline-block");
			$(".j-unclear").css("display","none");
		}
	}else if(tryType == $downloadFailed){
		if(status==0){
			status = "<font color=green>可重新下载</font>"
			$(".j-clear").css("display","inline-block");
			$(".j-unclear").css("display","none");
		}else if(status==1){
			status = "<font color=gray>队列中...</font>"
		}else if(status==-1){
			status = "<font color='#65A324'>下载中...</font>"
		}else if(status==2){
			status = "<font color=green>下载成功!</font>"
			$(".j-clear").css("display","inline-block");
			$(".j-unclear").css("display","none");
		}else if(status==3){
			status = "<font color=red>重新下载失败!</font>"
			$(".j-clear").css("display","inline-block");
			$(".j-unclear").css("display","none");
		}
	}
	return status;
}
function tryAgain(){
	var tryType = $("#tryAgainBtn").parent().attr("tryAgainType");
	var ids = "";
	$(".failedClass").each(function(){
		var v = $(this).val();
		ids += v+",";
	});
	if(ids.length==0){
		plugin.msgtip({type:"e",msg:"暂无可传输数据..."});
	}else{
		ids = ids.substring(0,ids.length-1);
		service.upDownloadRetry({
			ids:ids,
			type:tryType
		},function(data){
			var type = "e"
			if(data.code=="200"){
				type = "o"
			}
			plugin.msgtip({type:type,msg:data.msg});
		});
	}
}

function deleteFaileds(dsType){
	var tryType = $("#tryAgainBtn").parent().attr("tryAgainType");
	service2.deleteFailedByDsTypeAndType({
		dsType:dsType,
		type:tryType		
	}, function(data){
		plugin.msgtip({type:"i",msg:data.msg});
		var tryType = $("#tryAgainBtn").parent().attr("tryAgainType");
		$(".j-clear").css("display","none");
		$(".j-unclear").css("display","inline-block");
		$currentpage = 1;
		$("#w-page").val($currentpage);
		if(tryType == $uploadFailed){
			showUpDownloadFaileds($uploadFailed);
		}else{
			showUpDownloadFaileds($downloadFailed);
		}
	});
}

//function deleteFaileds(){
//	var ids = "";
//	$(".failedClass").each(function(){
//		var v = $(this).val();
//		ids += v+",";
//	});
//	if(ids.length==0){
//		plugin.msgtip({type:"e",msg:"没有可清空的数据了..."});
//	}else{
//		ids = ids.substring(0,ids.length-1);
//		service.deleteUpDownloadFailedFiles({
//			ids:ids
//		}, function(data){
//			plugin.msgtip({type:"i",msg:data.msg});
//			var tryType = $("#tryAgainBtn").parent().attr("tryAgainType");
//			$(".j-clear").css("display","none");
//			$(".j-unclear").css("display","inline-block");
//			if(tryType == $uploadFailed){
//				showUpDownloadFaileds($uploadFailed);
//			}else{
//				showUpDownloadFaileds($downloadFailed);
//			}
//		});
//	}
//}